import {
    addnewmessageapi,
    gethistorymessagedetailapi,
    getmsessageresidueapi,
    sendmessageapi,
} from '../../api/message'
import global from '../../../utils/global'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showtitleng: true,
        sendafter: 0,
        id: 0,
        sendNumber: 0,
        msessageresidue: 0,
        showpop: false,
        headline: "",
        content: "",
        number: 0,
        titlenum: 0,
        imgs: "",
        next: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        // console.log(options);
        // if (options.statue) {
        //     let res = await gethistorymessagedetailapi(options.id)
        //     console.log(res);
        //     this.setData({
        //         id: options.id,
        //         headline: res.data.messageTitle,
        //         content: res.data.messageContent,
        //         titlenum: res.data.messageTitle.length,
        //         number: res.data.messageContent.length,
        //         imgs: res.data.messageUrl,
        //         allowchange: true
        //     })
        // } else {
        //     this.setData({
        //         next: false
        //     })
        // }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    async next() {
        if (this.data.headline == '') {
            wx.showToast({
                title: '请输入标题',
                icon: "none"
            })
            return
        } else if (this.data.content == '') {
            wx.showToast({
                title: '请输入消息内容',
                icon: "none"
            })
            return
        } else {
            if (this.data.imgs === '') {
                let resoult = await addnewmessageapi({
                    messageTitle: this.data.headline,
                    messageContent: this.data.content,
                })
                console.log(resoult, '11111111111111111111');
                if (resoult.code == 200) {
                    wx.showToast({
                        title: '新建群发消息成功',
                        icon: "none"
                    })
                    wx.navigateTo({
                        url: '../choose-sending-range/choose-sending-range?id=' + resoult.data,
                    })
                } else {
                    wx.showToast({
                        title: '新建群发消息失败',
                        icon: "none"
                    })
                }
            } else {
                wx.uploadFile({
                    url: global.baseUrl + '/api/v2/zp/job/upload-file',
                    filePath: this.data.imgs,
                    name: 'file',
                    header: {
                        'Authorization': 'Wx ' + global.token
                    },
                    success: async (res) => {
                        console.log('上传成功', JSON.parse(res.data))
                        let data = JSON.parse(res.data)
                        let resoult = await addnewmessageapi({
                            messageTitle: this.data.headline,
                            messageContent: this.data.content,
                            messageUrl: data.data.fileUrl
                        })
                        console.log(resoult, '11111111111111111111');
                        if (resoult.code == 200) {
                            wx.showToast({
                                title: '新建群发消息成功',
                                icon: "none"
                            })
                            wx.navigateTo({
                                url: '../choose-sending-range/choose-sending-range?id=' + resoult.data,
                            })
                        } else {
                            wx.showToast({
                                title: '新建群发消息失败',
                                icon: "none"
                            })
                        }
                    },
                    fail(rej) {
                        console.log(rej, 'rej')
                    }
                })
            }
        }
    },
    changeheadline(e) {
        // console.log(e.detail.value);
        let len = e.detail.value.replace(/\s*/g, "").length
        if (len == 0) {
            this.setData({
                showtitleng: true
            })
        } else {
            this.setData({
                showtitleng: false
            })
        }
        this.setData({
            titlenum: len,
            headline: e.detail.value
        })
    },
    changecontent(e) {
        let len = e.detail.value.replace(/ */g, "").length
        this.setData({
            number: len,
            content: e.detail.value.replace(/ */g, "")
        })
    },
    upfile() {
        // console.log('上传图片');
        wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album', 'camera'],
            camera: 'back',
            sizeType: ['original', 'compressed'],
            success: (res) => {
                console.log(res, '返回的文件')
                if (res.tempFiles[0].fileType == 'video') {
                    wx.showToast({
                        title: '文件格式不符请重新选择',
                        icon: "none"
                    })
                    return
                }
                if (res.tempFiles[0].size/1024 > 500) {
                    wx.showToast({
                        title: '文件格式过大请重新选择',
                        icon: "none"
                    })
                    return
                }
                console.log(res.tempFiles[0].tempFilePath)
                this.setData({
                    imgs: res.tempFiles[0].tempFilePath,
                    haschangeimg: true
                })
            }
        })
    },
    clearimg() {
        this.setData({
            imgs: ""
        })
    },
    // async issue() {
    //     this.setData({
    //         showpop: true
    //     })
    //     let res = await getmsessageresidueapi()
    //     // console.log(res.data.smsBalance);
    //     let res2 = await gethistorymessagedetailapi(this.data.id)
    //     console.log(res2);
    //     this.setData({
    //         msessageresidue: res.data.smsBalance,
    //         sendNumber: res2.data.sendNumber,
    //         sendafter: res.data.smsBalance - res2.data.sendNumber
    //     })
    // },
    // popcancel() {
    //     this.setData({
    //         showpop: false
    //     })
    //     wx.relaunch({
    //       url: '/packageMassmessage/pages/message-index/message-index',
    //     })
    // },
    // async sendmessage() {
    //     let res = await sendmessageapi({
    //         id: this.data.id,
    //         appId: "wx850e3a22db40acac"
    //     })
    //     if (res.code == 200) {
    //         wx.redirectTo({
    //             url: '../../pages/send-success/send-success',
    //         })
    //     }
    // },
    examine() {
        console.log('查看群发消息人员');
    },
    tomessagepay() {
        wx.navigateTo({
            url: '/packageMassmessage/pages/message-recharge/message-recharge',
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})